package com.csoa.community.adapter;

import java.util.List;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.collect.community.R;
import com.csoa.community.activity.DataStatisticsActivity;
import com.csoa.community.bean.DataStatisticsBean;
import com.csoa.community.utils.StringUtils;

public class DataStaticticsAdapter extends BaseAdapter {

	private DataStatisticsActivity context;
	private LayoutInflater mInflater;
	private List<DataStatisticsBean> mItemList;

	public DataStaticticsAdapter(DataStatisticsActivity dataStatistics,
			List<DataStatisticsBean> lists) {
		super();
		this.context = dataStatistics;
		this.mItemList = lists;
		mInflater = LayoutInflater.from(dataStatistics);
	}

	@Override
	public int getCount() {
		return mItemList == null ? 0 : mItemList.size();
	}

	@Override
	public DataStatisticsBean getItem(int arg0) {
		// TODO Auto-generated method stub
		return mItemList == null ? null : mItemList.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(final int index, View view, ViewGroup arg2) {
		View tempItemView = null;
		ViewHolder viewHolder = null;

		if (view == null) {
			tempItemView = mInflater.inflate(
					R.layout.list_item_data_statistics, null);
			viewHolder = new ViewHolder();
			viewHolder.gridNumber = (TextView) tempItemView
					.findViewById(R.id.tvGridNumber);
			viewHolder.chineseName = (TextView) tempItemView
					.findViewById(R.id.tvChineseName);
			viewHolder.latinName = (TextView) tempItemView
					.findViewById(R.id.tvLatinName);
			viewHolder.quadratsNumber = (TextView) tempItemView
					.findViewById(R.id.tvYfNumber);
			viewHolder.type = (TextView) tempItemView.findViewById(R.id.tvType);
			viewHolder.surveyMethod = (TextView) tempItemView
					.findViewById(R.id.tvSurWay);
			viewHolder.surveyArea = (TextView) tempItemView
					.findViewById(R.id.tvSurArea);
			tempItemView.setTag(viewHolder);
		} else {
			tempItemView = view;
			viewHolder = (ViewHolder) tempItemView.getTag();
		}

		if (mItemList.size() > 1) {
			if (index == 0) {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_top);
			} else if (index == mItemList.size() - 1) {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_bottom);
			} else {
				tempItemView
						.setBackgroundResource(R.drawable.background_view_rounded_middle);
			}

		} else if (mItemList.size() == 1) {
			tempItemView
					.setBackgroundResource(R.drawable.background_view_rounded_single);
		}

		DataStatisticsBean data = getItem(index);
		if (StringUtils.isEmpty(data.gridNumber)) {
			data.gridNumber = "暂无编号";
		}
		viewHolder.gridNumber.setText(Html
				.fromHtml("<html><font color='red'>编号:</font></html>")
				+ data.gridNumber);
		viewHolder.chineseName.setText("中文名:" + data.chineseName);
		viewHolder.latinName.setText(Html
				.fromHtml("<html><font color='red'>拉丁学名:</font></html>")
				+ data.latinName);
		if (StringUtils.isEmpty(data.quadratsNumber)) {
			viewHolder.quadratsNumber.setVisibility(View.GONE);
		}else {
			viewHolder.quadratsNumber.setText("样方号:" + data.quadratsNumber);
		}

		viewHolder.type.setText(Html
				.fromHtml("<html><font color='red'>物种类别:</font></html>")
				+ data.type);
		viewHolder.surveyMethod.setText("调查方法:" + data.surveyMethod);
		viewHolder.surveyArea.setText(Html
				.fromHtml("<html><font color='red'>调查区域:</font></html>")
				+ data.surveyArea);

		tempItemView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				showDefineDialog(index);
			}
		});

		return tempItemView;
	}

	private static final class ViewHolder {
		TextView gridNumber;
		TextView chineseName;
		TextView latinName;
		TextView quadratsNumber;
		TextView type;
		TextView surveyMethod;
		TextView surveyArea;
	}

	private void showDefineDialog(final int position) {
		// String items[] = new String[] { "查看物种名录", "添加物种名录", "删除物种名录",
		// "修改物种名录"
		// };
		//
		// CommonDialog caculatorDialog = new CommonDialog(context);
		// // caculatorDialog.setDialogTitle("常用语");
		// caculatorDialog.setItems(items, new OnListItemClickListener() {
		//
		// @Override
		// public void onListItemClick(int which) {
		//
		// }
		// });
		// caculatorDialog.show();
	}

}
